home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / luminance16.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  4.4 KB  |  155 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1997. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* molehill uses the GLU NURBS routines to draw some nice surfaces. */
  9.  
  10. #include <GL/glut.h>
  11.  
  12. GLfloat mat_red_diffuse[] = { 0.7, 0.0, 0.1, 1.0 };
  13. GLfloat mat_green_diffuse[] = { 0.0, 0.7, 0.1, 1.0 };
  14. GLfloat mat_blue_diffuse[] = { 0.0, 0.1, 0.7, 1.0 };
  15. GLfloat mat_yellow_diffuse[] = { 0.7, 0.8, 0.1, 1.0 };
  16. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  17. GLfloat mat_shininess[] = { 100.0 };
  18. GLfloat knots[8] = { 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 };
  19. GLfloat pts1[4][4][3], pts2[4][4][3];
  20. GLfloat pts3[4][4][3], pts4[4][4][3];
  21. GLUnurbsObj *nurb;
  22. int u, v;
  23.  
  24. static void 
  25. display(void)
  26. {
  27.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  28.   glCallList(1);
  29.   glFlush();
  30. }
  31.  
  32. int 
  33. main(int argc, char **argv)
  34. {
  35.   glutInit(&argc, argv);
  36.   glutInitDisplayString("luminance depth");
  37.   glutCreateWindow("molehill");
  38.   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  39.   glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  40.   glEnable(GL_LIGHTING);
  41.   glEnable(GL_LIGHT0);
  42.   glEnable(GL_DEPTH_TEST);
  43.   glEnable(GL_AUTO_NORMAL);
  44.   glEnable(GL_NORMALIZE);
  45.   nurb = gluNewNurbsRenderer();
  46.   gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 25.0);
  47.   gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL);
  48.  
  49.   /* Build control points for NURBS mole hills. */
  50.   for(u=0; u<4; u++) {
  51.     for(v=0; v<4; v++) {
  52.       /* Red. */
  53.       pts1[u][v][0] = 2.0*((GLfloat)u);
  54.       pts1[u][v][1] = 2.0*((GLfloat)v);
  55.       if((u==1 || u == 2) && (v == 1 || v == 2))
  56.     /* Stretch up middle. */
  57.     pts1[u][v][2] = 6.0;
  58.       else
  59.     pts1[u][v][2] = 0.0;
  60.  
  61.       /* Green. */
  62.       pts2[u][v][0] = 2.0*((GLfloat)u - 3.0);
  63.       pts2[u][v][1] = 2.0*((GLfloat)v - 3.0);
  64.       if((u==1 || u == 2) && (v == 1 || v == 2))
  65.     if(u == 1 && v == 1) 
  66.       /* Pull hard on single middle square. */
  67.       pts2[u][v][2] = 15.0;
  68.     else
  69.       /* Push down on other middle squares. */
  70.       pts2[u][v][2] = -2.0;
  71.       else
  72.     pts2[u][v][2] = 0.0;
  73.  
  74.       /* Blue. */
  75.       pts3[u][v][0] = 2.0*((GLfloat)u - 3.0);
  76.       pts3[u][v][1] = 2.0*((GLfloat)v);
  77.       if((u==1 || u == 2) && (v == 1 || v == 2))
  78.     if(u == 1 && v == 2)
  79.       /* Pull up on single middple square. */
  80.       pts3[u][v][2] = 11.0;
  81.     else
  82.       /* Pull up slightly on other middle squares. */
  83.       pts3[u][v][2] = 2.0;
  84.       else
  85.     pts3[u][v][2] = 0.0;
  86.  
  87.       /* Yellow. */
  88.       pts4[u][v][0] = 2.0*((GLfloat)u);
  89.       pts4[u][v][1] = 2.0*((GLfloat)v - 3.0);
  90.       if((u==1 || u == 2 || u == 3) && (v == 1 || v == 2))
  91.     if(v == 1) 
  92.       /* Push down front middle and right squares. */
  93.       pts4[u][v][2] = -2.0;
  94.     else
  95.       /* Pull up back middle and right squares. */
  96.       pts4[u][v][2] = 5.0;
  97.       else
  98.     pts4[u][v][2] = 0.0;
  99.     }
  100.   }
  101.   /* Stretch up red's far right corner. */
  102.   pts1[3][3][2] = 6;
  103.   /* Pull down green's near left corner a little. */
  104.   pts2[0][0][2] = -2;
  105.   /* Turn up meeting of four corners. */
  106.   pts1[0][0][2] = 1;
  107.   pts2[3][3][2] = 1;
  108.   pts3[3][0][2] = 1;
  109.   pts4[0][3][2] = 1;
  110.  
  111.   glMatrixMode(GL_PROJECTION);
  112.   gluPerspective(55.0, 1.0, 2.0, 24.0);
  113.   glMatrixMode(GL_MODELVIEW);
  114.   glTranslatef(0.0, 0.0, -15.0);
  115.   glRotatef(330.0, 1.0, 0.0, 0.0);
  116.  
  117.   glNewList(1, GL_COMPILE);
  118.     /* Render red hill. */
  119.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_red_diffuse);
  120.     gluBeginSurface(nurb);
  121.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  122.         4 * 3, 3, &pts1[0][0][0],
  123.         4, 4, GL_MAP2_VERTEX_3);
  124.     gluEndSurface(nurb);
  125.  
  126.     /* Render green hill. */
  127.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
  128.     gluBeginSurface(nurb);
  129.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  130.         4 * 3, 3, &pts2[0][0][0],
  131.         4, 4, GL_MAP2_VERTEX_3);
  132.     gluEndSurface(nurb);
  133.  
  134.     /* Render blue hill. */
  135.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_blue_diffuse);
  136.     gluBeginSurface(nurb);
  137.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  138.         4 * 3, 3, &pts3[0][0][0],
  139.         4, 4, GL_MAP2_VERTEX_3);
  140.     gluEndSurface(nurb);
  141.  
  142.     /* Render yellow hill. */
  143.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_yellow_diffuse);
  144.     gluBeginSurface(nurb);
  145.       gluNurbsSurface(nurb, 8, knots, 8, knots,
  146.         4 * 3, 3, &pts4[0][0][0],
  147.         4, 4, GL_MAP2_VERTEX_3);
  148.     gluEndSurface(nurb);
  149.   glEndList();
  150.  
  151.   glutDisplayFunc(display);
  152.   glutMainLoop();
  153.   return 0;             /* ANSI C requires main to return int. */
  154. }
  155.